<template>
	<view>
		<uni-segmented-control @clickItem="e=>{current=e.currentIndex}" :current="current" :values="['搜索','详情','内容']"
			styleType="text" activeColor="#007aff">
		</uni-segmented-control>
		<!-- <button type="primary" @click="save()" style="margin: 20rpx;">保存</button> -->
		<view v-if="current==0">
			<uni-forms label-align="center" :modelValue="formData" label-position="left">
				<uni-forms-item label="HOST">
					<uni-easyinput type="text" v-model="formData.host" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="网站名称">
					<uni-easyinput type="text" v-model="formData.name" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="搜索地址">
					<uni-easyinput type="text" v-model="formData.search_url" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="方法">
					<uni-easyinput type="text" v-model="formData.method" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="Header">
					<uni-easyinput type="text" v-model="formData.header" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="KEY">
					<uni-easyinput type="text" v-model="formData.search_key" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="编码">
					<uni-easyinput type="text" v-model="formData.charset" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="WORD">
					<uni-easyinput type="text" v-model="formData.word" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="搜索表单">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.search_form"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="searchTest(0)">测试</button>

				<uni-forms-item label="内容正则">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.list_content_zz"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="searchTest(1)">测试</button>
				<uni-forms-item label="列表正则">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.item_zz"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="searchTest(2)">测试</button>
			</uni-forms>
		</view>
		<view v-if="current==1">
			<uni-forms label-align="center" :modelValue="formData" label-position="left">
				<uni-forms-item label="链接">
					<uni-easyinput type="text" v-model="formData.url" placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="detailTest(0)">测试内容</button>
				<uni-forms-item label="简介正则">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.description_zz"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="detailTest(1)">测试简介</button>
				<uni-forms-item label="图片正则">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.image_zz"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="detailTest(2)">测试图片</button>

				<uni-forms-item label="类型正则">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.type_zz"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="detailTest(3)">测试类型</button>

				<uni-forms-item label="完整目录">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.all_mulu_zz"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="detailTest(6)">测试完整目录</button>

				<uni-forms-item label="最新章节">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.lastest_chapter_zz"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="detailTest(8)">测试最新章节</button>

				<uni-forms-item label="下一页">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.list_next_zz"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="detailTest(7)">测试下一页</button>

				<uni-forms-item label="内容正则">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.list_pattern"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="detailTest(4)">测试内容正则</button>

				<uni-forms-item label="列表正则">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.chapter_pattern"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="detailTest(5)">测试列表正则</button>
			</uni-forms>
		</view>
		<view v-if="current==2">
			<uni-forms label-align="center" :modelValue="formData" label-position="left">
				<uni-forms-item label="链接">
					<uni-easyinput type="text" v-model="formData.url" placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="contentTest(0)">测试内容</button>
				<uni-forms-item label="内容正则">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.content_pattern"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="contentTest(1)">测试章节内容</button>
				<uni-forms-item label="替换">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.replaceList"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="contentTest(3)"
					@longpress='formData.replaceList=JSON.parse(formData.replaceList);formData.replaceList.push({"key":"","value":""});formData.replaceList=JSON.stringify(formData.replaceList);'>测试替换</button>

				<uni-forms-item label="下一页">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="formData.content_next_zz"
						placeholder="请输入" />
				</uni-forms-item>
				<button type="primary" @click="contentTest(2)">测试下一页</button>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	// let request = (url, method = 'GET', data = {}, dataType = "json", header = {}) => {
	import request from '../../utils/request.js'
	import request2 from '../../utils/request2.js'
	import {
		addWebsite,
		getWebsiteById
	} from '../../api/index.js'
	export default {
		data() {
			return {
				current: 0,
				formData: {
					method: "GET"
				},
				id: null,
			};
		},
		watch: {
			'formData.method': {
				handler: function(n, o) {
					if (n == "POST") {
						this.formData.header = JSON.stringify({
							"Content-Type": "application/x-www-form-urlencoded"
						})
					}
				},
				immediate: false
			}
		},
		onLoad(e) {
			if (e.id) {
				this.id = e.id
				getWebsiteById(e.id).then(res => {
					this.formData = res
					this.formData.header = JSON.stringify(this.formData.header)
					this.formData.search_form = JSON.stringify(this.formData.search_form)
					this.formData.replaceList = JSON.stringify(this.formData.replaceList)
				})
			} else {
				uni.navigateBack()
			}
		},
		methods: {
			searchTest(index) {
				this.save()
				let search_url = this.formData.search_url
				let method = this.formData.method
				let header = JSON.parse(this.formData.header)
				let key = this.formData.search_key
				let data = JSON.parse(this.formData.search_form)
				data[key] = this.formData.word
				request(search_url, method, data, "", header).then(res => {
					if (index == 0) {
						console.log(res);
						return
					}
					let list_content_zz = this.formData.list_content_zz
					let r = new RegExp(list_content_zz, 'gs')
					let list_content = res.match(r)[0]
					if (index == 1) {
						console.log(r.exec(res)[0]);
						return
					}
					let item_zz = this.formData.item_zz
					let a = {
						name: '链接',
						num: item_zz.indexOf('[链接]')
					}
					let b = {
						name: '名称',
						num: item_zz.indexOf('[名称]')
					}
					let c = {
						name: '作者',
						num: item_zz.indexOf('[作者]')
					}
					item_zz = item_zz.replace('[链接]', '(.*?)').replace('[名称]', '(.*?)').replace('[作者]', '(.*?)')
					r = new RegExp(item_zz, 'gs')
					let d = [a, b, c]
					let e = {}
					d.sort(function(i, j) {
						return i.num > j.num
					})
					for (let i = 0; i < d.length; i++) {
						e[d[i].name] = i
					}
					let list = list_content.matchAll(r)
					for (let s of list) {
						let o = {
							'url': s[e['链接'] + 1].replace(/\n*/g, '').replace(/ /g, ''),
							'name': s[e['名称'] + 1].replace(/\n*/g, '').replace(/ /g, ''),
							'author': s[e['作者'] + 1].replace(/\n*/g, '').replace(/ /g, '').replace(/<.*?>/g,
								''),
						}
						if (this.formData.host.indexOf('https://') > -1 && o.url.indexOf('https://') == -1) {
							o.url = o.url.replace('http://', 'https://')
						} else if (this.formData.host.indexOf('http://') > -1 && o.url.indexOf('http://') == -1) {
							o.url = o.url.replace('http://s', 'https://')
						}
						if (index == 2) {
							console.log(o);
						}
					}
				})
			},
			detailTest(index) {
				this.save()
				let url = this.formData.url
				request(url).then(res => {
					let r;
					if (index == 0) {
						console.log(res);
						return
					}

					if (index == 1) {
						let description_zz = this.formData.description_zz
						r = new RegExp(description_zz, 'gs')
						let description = r.exec(res)[1]
						console.log(description);
						return
					}


					if (index == 2) {
						let image_zz = this.formData.image_zz
						r = new RegExp(image_zz, 'gs')
						let image = r.exec(res)[1]
						console.log(image);
						return
					}

					if (index == 3) {

						let type_zz = this.formData.type_zz
						r = new RegExp(type_zz, 'gs')
						let type = r.exec(res)[1]
						console.log(type);
						return
					}

					if (index == 8) {
						let lastest_chapter_zz = this.formData.lastest_chapter_zz
						r = new RegExp(lastest_chapter_zz, 'gs')
						let lastest_chapter = r.exec(res)[1]
						console.log(lastest_chapter);
						return
					}

					if (index == 7) {
						let list_next_zz = this.formData.list_next_zz
						r = new RegExp(list_next_zz, 'gs')
						let next_list = r.exec(res)[1]
						console.log(next_list);
						return
					}

					if (this.formData.all_mulu_zz) {
						r = new RegExp(this.formData.all_mulu_zz, 'gs')
						let next = r.exec(res)[1]
						if (next.indexOf(this.formData.host) == -1) {
							if (next[0].charCodeAt() == 47) {
								next = this.formData.host + next
							} else {
								next = this.formData.url + next
							}
						}
						if (index == 6) {
							console.log(next);
							return
						}
						request(next).then(res => {
							let list_pattern = this.formData.list_pattern
							r = new RegExp(list_pattern, 'gs')
							let list_content = r.exec(res)[1]
							if (index == 4) {
								console.log(list_content);
								return
							}

							let chapter_pattern = this.formData.chapter_pattern.replace('[链接]', '(.*?)')
								.replace('[章节]',
									'(.*?)')
							r = new RegExp(chapter_pattern, 'gs')
							let list = list_content.matchAll(r)
							if (index == 5) {
								for (let s of list) {

									console.log(s);
									return
								}
							}
						})
						return
					}

					let list_pattern = this.formData.list_pattern
					r = new RegExp(list_pattern, 'gs')
					let list_content = r.exec(res)[1]
					if (index == 4) {
						console.log(list_content);
						return
					}

					let chapter_pattern = this.formData.chapter_pattern.replace('[链接]', '(.*?)').replace('[章节]',
						'(.*?)')
					r = new RegExp(chapter_pattern, 'gs')
					let list = list_content.matchAll(r)
					if (index == 5) {
						for (let s of list) {
							console.log(s[1], s[2]);
						}
						return
					}
				})
			},
			contentTest(index) {
				let url = this.formData.url
				this.save()
				request(url).then(res => {
					if (index == 0) {
						console.log(res);
						return
					}
					let r = new RegExp(this.formData.content_pattern, 'gs')
					if (index == 1) {
						console.log(res.match(r)[0]);
					}
					if (index == 3) {
						let c = res.match(r)[0]
						let List = JSON.parse(this.formData.replaceList)
						for (let s of List) {
							r = new RegExp(s.key, 'gsu')
							c = c.replace(r, s.value)
						}

						console.log(c);
						return
					}
					r = new RegExp(this.formData.content_next_zz, 'gs')
					if (index == 2) {
						console.log(r.exec(res)[1]);
					}
				})
			},
			save() {
				let data = Object.assign({}, this.formData)
				data.id = this.id
				for (let key in data) {
					if (!data[key]) {
						delete data[key]
					}
				}
				addWebsite(data).then(res => {
					wx.showToast({
						icon: 'none',
						title: '保存成功'
					})
				})
			},
		}
	}
</script>

<style lang="less">
	.uni-forms {
		padding: 20rpx;
	}

	uni-button {
		margin: 20rpx 0;
	}
</style>
